<template>
  <div class="course-button">
    <el-button type="success" plain size="mini" @click="handlePreview(row)">
      预览
    </el-button>
    <!-- <el-button type="primary" plain size="mini" @click="handleExport(row)">
      导出
    </el-button> -->
    <!-- <el-button type="primary" plain size="mini" @click="handleState(row)">
      发布课程
    </el-button> -->
    <!-- 提交审核 -->
    <!-- <template v-for="(btn, idx) in auditBtn">
      <el-button
        :key="btn"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          userId == row.createBy &&
          row.status == 0 &&
          row.auditStatus != 4
        "
        plain
        size="mini"
        @click="handleAudit(row)"
        >提交审核</el-button
      >
    </template> -->
    <!-- 撤回审核 -->
    <!-- <template v-for="(btn, idx) in auditBtn">
      <el-button
        :key="btn"
        v-if="
          $button(btn) &&
          userId == row.createBy &&
          type == idx + 1 &&
          row.status == 0 &&
          row.auditStatus == 4
        "
        plain
        size="mini"
        @click="handleReAudit(row)"
        >撤回审核</el-button
      >
    </template> -->
    <!-- 下架课程 -->
    <el-button
      v-if="row.status == 1"
      plain
      size="mini"
      @click="handleSoldOut(row)"
    >
      下架
    </el-button>
    <!-- 上架课程 -->
    <el-button
      v-if="row.status != 1"
      plain
      size="mini"
      @click="handlePublish(row)"
    >
      发布
    </el-button>

    <!-- 驳回课程 -->
    <!-- <template v-for="(btn, idx) in rejectBtns">
      <el-button
        :key="btn"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          row.status == 1 &&
          row.auditStatus == 2
        "
        plain
        size="mini"
        type="danger"
        @click="handleReject(row)"
      >
        驳回
      </el-button>
    </template> -->
    <!-- 删除课程 -->
    <template v-for="(btn, idx) in delBtns">
      <el-button
        :key="btn"
        v-if="$button(btn) && type == idx + 1"
        :disabled="row.status == 1"
        type="danger"
        size="mini"
        plain
        @click="handleDelete(row)"
        >删除</el-button
      >
    </template>
    <!-- 推荐 -->
    <!-- <template v-for="(btn, idx) in cmdBtns">
      <el-button
        :key="btn"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          row.recommended != 1 &&
          row.status == 1 &&
          row.auditStatus == 2
        "
        plain
        size="mini"
        @click="handleRecommend(row, 1)"
        >推荐</el-button
      >
    </template> -->
    <!-- 取消推荐 -->
    <!-- <template v-for="(btn, idx) in cmdBtns">
      <el-button
        :key="btn"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          row.recommended == 1 &&
          row.status == 1 &&
          row.auditStatus == 2
        "
        plain
        size="mini"
        @click="handleRecommend(row, 0)"
        >取消推荐</el-button
      >
    </template> -->
    <!-- 设为优质资源 -->
    <!-- <template v-for="(btn, idx) in fineBtns">
      <el-button
        :key="btn"
        plain
        size="mini"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          row.selection != 1 &&
          row.status == 1 &&
          row.auditStatus == 2
        "
        @click="handleSelection(row, 1)"
        >设为优质资源</el-button
      >
    </template> -->
    <!-- 取消优质资源 -->
    <!-- <template v-for="(btn, idx) in fineBtns">
      <el-button
        :key="btn"
        plain
        size="mini"
        v-if="
          $button(btn) &&
          type == idx + 1 &&
          row.selection == 1 &&
          row.status == 1 &&
          row.auditStatus == 2
        "
        @click="handleSelection(row, 0)"
        >取消优质资源</el-button
      >
    </template> -->
  </div>
</template>
<script>
// auditStatus 审核状态：1-待审核（暂存），2-审核通过，3-不通过 4，审核中
// status 课程状态：0-未发布，1-已发布 2-下架
import {
  sysCourseDelete,
  sysCourseUpdate,
  sysCourseSelectionCourse,
  openApiExportWordExpCourseCollect,
} from '@/api/admin/courseLibrary/sysCourse.js'
import { flowApiTakeBack } from '@/api/admin/audit/flowApi.js'
import coursePreview from '@/mixins/coursePreview'
import { mapGetters } from 'vuex'
import Bus from '@/utils/bus.js'
export default {
  mixins: [coursePreview],
  props: ['row'],
  data() {
    return {
      type: this.$route.meta.type,
      auditBtn: [723, 724, 725, 732, 733, 734],
      delBtns: [698, 721, 722, 729, 730, 731],
      cmdBtns: [820, 821, 822, 823, 824, 825],
      underBtns: [827, 828, 829, 830, 831, 833],
      rejectBtns: [854, 855, 856, 857, 858, 859],
      fineBtns: [860, 861, 862, 863, 864, 865],
      listQuery: {
        businessKey: '',
      },
    }
  },
  computed: mapGetters(['userId', 'userType']),
  methods: {
    handleState(row) {
      let { id, status } = row
      let obj = {
        id: id,
        status: status == 0 ? 1 : 0,
      }
      // console.log(obj);
      // return
      sysCourseUpdate(obj).then((res) => {
        if (!res || res.data.code != 0) return
        Bus.$emit('refreshCourseList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    handleExport(row) {
      let url = '/openApi/exportWord/expCourseCollect/' + row.id
      window.location.href = this.baseURL + url
    },
    //推荐
    handleRecommend(row, type) {
      Bus.$emit('showDialogCourseRecommend', row, type)
    },
    //提交审核
    handleAudit(row) {
      Bus.$emit('showDialogCourseAudit', row)
    },
    //设为优质资源
    handleSelection(row, type) {
      sysCourseSelectionCourse({ id: row.id, selection: type }).then((res) => {
        Bus.$emit('refreshCourseList')
      })
    },
    //撤回审核
    handleReAudit(row) {
      this.listQuery.businessKey = row.courseType + ':' + row.id
      flowApiTakeBack(this.listQuery).then((res) => {
        if (!res || res.data.code != 0) return
        Bus.$emit('refreshCourseList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //下架确认
    handleSoldOut(row) {
      this.$confirm('确定要下架该课程吗?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.soldOut(row, 2)
        })
        .catch(() => {})
    },
    //下架status=2
    soldOut(row, status) {
      let data = {
        id: row.id,
        status,
        auditStatus: 2,
      }
      // if (status == 0) {
      //   data.auditStatus = 1
      // } else {
      //   delete data.auditStatus
      // }
      sysCourseUpdate(data).then((res) => {
        if (!res || res.data.code != 0) return
        Bus.$emit('refreshCourseList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysCourseDelete(row.id).then((res) => {
        if (!res || res.data.code != 0) return
        Bus.$emit('refreshCourseList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //发布
    handlePublish(row) {
      this.$confirm('确定要发布该课程吗?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.soldOut(row, 1)
        })
        .catch(() => {})
    },
    //驳回
    handleReject(row) {
      this.$confirm('确定要驳回该课程吗?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.soldOut(row, 0)
        })
        .catch(() => {})
    },
  },
}
</script>
<style scoped lang="scss">
</style>